package com.runfast.waimai.web.controller.Driver_controller;

import com.runfast.paotui.dao.model.Order;
import com.runfast.paotui.dao.model.RunfastCuser;
import com.runfast.paotui.dao.model.RunfastShopper;
import com.runfast.paotui.service.OrderService;
import com.runfast.paotui.service.RunfastCuserService;
import com.runfast.paotui.service.RunfastShopperService;
import com.runfast.waimai.dao.model.RunfastBusiness;
import com.runfast.waimai.dao.model.RunfastGoodsSellRecord;
import com.runfast.waimai.service.RunfastBusinessService;
import com.runfast.waimai.service.RunfastGoodsSellRecordService;
import com.runfast.waimai.util.MsgPushUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/shopper/*")
public class ShopperOrderFirstController extends DriverBaseController {
    //跑腿定单信息
    @Resource
    OrderService orderService;
    @Resource
    private RunfastGoodsSellRecordService goodsSellRecordService;
    //RunfastGoodsSellRecord// 外卖商品订单
    @Resource
    private RunfastBusinessService businessService;
    //RunfastBusiness商家信息
    @Resource
    private RunfastShopperService shopperService;
    //骑手信息
    @Resource
    private RunfastCuserService cuserService;
    ////用户信息

    protected static Logger LOG = LoggerFactory.getLogger(MsgPushUtils.class);

    @RequestMapping(value = "businessmsg", method = RequestMethod.POST)//获得商家信息
    public @ResponseBody
    ModelMap businessmsg(
            Integer id) {
        RunfastBusiness runfastBusiness = businessService.findById(id);//通过商家id获取商家信息
        ModelMap model = new ModelMap();
        model.put("runfastBusiness", runfastBusiness);
        return model;
    }

    ////用户信息
    @RequestMapping(value = "cusermsg", method = RequestMethod.POST)//获得商家信息
    public @ResponseBody
    ModelMap cusermsg(
            Integer id) {
        RunfastCuser cuser = cuserService.get(id);//通过商家id获取商家信息
        ModelMap model = new ModelMap();
        model.put("cuser", cuser);
        return model;
    }

    //显示 1为新任务 2 已取货 3为待送达 4 为异常单
    @RequestMapping(value = "index", method = RequestMethod.POST)
    public @ResponseBody ModelMap index(
            String token, //骑手id
            Integer a ,Integer b) { //a1为新任务 a2 已取货 a3为待送达 a4 为异常单
                                    //b 1为跑腿异常定单  2 外卖异常单

        ModelMap model = new ModelMap();
        if(b==1){
            if(a==1){
                if (super.getLoginShopper(token) != null) {//配送员信息不能为空
                    //通过商家id 用户id 骑手id  查询商家信息
                    //通过商家id 用户id 骑手id 查询买家信息
                    //通过外卖商品订单商家 骑手id ,定单号，定单状态  查询为新任务
                    List<Order> orders= orderService.findByShopperidAndStatus(super.getLoginShopper(token).getId(),0);// //a1为新任务
                    //订单状态编码-3:商家拒单-1：订单取消  0：客户下单，1：客户已付款  2：商家接单  3：骑手接单   4：商品打包 ，5：商品配送 6：商品送达，7:确认收货 ，8：订单完成 9.异常单
                    if(orders==null){
                        model.put("msg","目前没有骑手接跑腿定单");
                    }
                    model.put("orders",orders);
                }
            }
            if(a==2){
                if (super.getLoginShopper(token) != null) {//配送员信息不能为空
                    List<Order> orders= orderService.findByShopperidAndStatus(super.getLoginShopper(token).getId(),2);//a2 已取货
                    if(orders==null){
                        model.put("msg","没有商品要取");
                    }
                    model.put("orders",orders);

                }
            }
            if(a==3){
                if (super.getLoginShopper(token) != null) {//配送员信息不能为空
                    List<Order> orders= orderService.findByShopperidAndStatus(super.getLoginShopper(token).getId(),2);//a3为待送达
                    if(orders==null){
                        model.put("msg","没有要送的跑腿定单");
                    }
                    model.put("orders",orders);

                }
            }
            if(a==4){
                if(super.getLoginShopper(token)!=null){
                    List<Order> orders = orderService.findByShopperidAndStatus(super.getLoginShopper(token).getId(),  9);//跑腿目前没有异常单
                    if(orders==null){
                        model.put("msg","没有异常单");
                    }model.put("orders",orders);
                }
            }
        }
        if(b==2) {
        if(a==1){
            if (super.getLoginShopper(token) != null) {//配送员信息不能为空
                //通过商家id 用户id 骑手id  查询商家信息
                //通过商家id 用户id 骑手id 查询买家信息
                //通过外卖商品订单商家 骑手id ,定单号，定单状态  查询为新任务
               List<RunfastGoodsSellRecord> goodsSellRecord= goodsSellRecordService.findByShopperIdAndStruts(super.getLoginShopper(token).getId(),3);// //a1为新任务
                //订单状态编码-3:商家拒单-1：订单取消  0：客户下单，1：客户已付款  2：商家接单  3：骑手接单   4：商品打包 ，5：商品配送 6：商品送达，7:确认收货 ，8：订单完成 9.异常单
                      if(goodsSellRecord==null){
                          model.put("msg","没有外卖新任务");
                      }
                        model.put("list",goodsSellRecord);
                 }
        }
        if(a==2){
            if (super.getLoginShopper(token) != null) {//配送员信息不能为空
                List<RunfastGoodsSellRecord> goodsSellRecord= goodsSellRecordService.findByShopperIdAndStruts(super.getLoginShopper(token).getId(),4);//a2 已取货
               if(goodsSellRecord==null){
                   model.put("msg","没有商品要取");
               }
                model.put("list",goodsSellRecord);

            }
        }
        if(a==3){
            if (super.getLoginShopper(token) != null) {//配送员信息不能为空
                List<RunfastGoodsSellRecord> goodsSellRecord= goodsSellRecordService.findByShopperIdAndStruts(super.getLoginShopper(token).getId(),5);//a3为待送达
                if(goodsSellRecord==null){
                    model.put("msg","没有要送的单");
                }
                model.put("list",goodsSellRecord);

            }
        }
        if(a==4){
            if (super.getLoginShopper(token) != null) {
                //配送员信息不能为空
                //判断一哈中是跑腿 还是外卖

             //      List<Order> list = orderService.findByShopperidAndStatus(super.getLoginShopper().getId(),  9);
               /*     List<RunfastGoodsSellRecord> list= goodsSellRecordService.findByShopperIdAndStrutsAndagree1(super.getLoginShopper().getId(), 9 );//查询里面加了agree=9 为跑腿定单
                    if (list == null) {
                        model.put("msg", "没有异常单");

                    } else {
                        model.put("list", list);
                    }*/
                List<RunfastGoodsSellRecord> goodsSellRecord = goodsSellRecordService.findByShopperIdAndStrutsAndagree2(super.getLoginShopper(token).getId(), 9);//agree<9 就是除了跑腿外的异常
                if (goodsSellRecord == null) {
                    model.put("msg", "没有异常单");
                }
                model.put("list", goodsSellRecord);
            }
               /* List<RunfastShopper> shopper = shopperService.findByIdError(super.getLoginShopper(), -1);//1为异常信息 ，查询所有异常定单
                model.put("shopper", shopper);
                if(shopper !=null){
                    Integer i=  shopperService.findByIdErrorCount(super.getLoginShopper(),-1);//查询异常单的数量
                    model.put("errormsg",i );
                }*/
            }
        }

        return model;
    }
}